package com.vehicle.service.ServiceImpl;

import com.vehicle.mapper.CarMapper;
import com.vehicle.mapper.CustomerMapper;
import com.vehicle.mapper.RentalsMapper;
import com.vehicle.pojo.Cars;
import com.vehicle.pojo.Customers;
import com.vehicle.pojo.Rentals;
import com.vehicle.pojo.VO.CustomersVO;
import com.vehicle.pojo.VO.OrderVO;
import com.vehicle.result.PageResult;
import com.vehicle.service.SelfMessageService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service

public class SelfMessageControllerImpl implements SelfMessageService {
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private RentalsMapper rentalsMapper;
    @Autowired
    private CarMapper carMapper;
    /**
     * 个人中心->根据ID查询基本信息
     * @param id 路径参数
     * @return
     */
    @Override
    public CustomersVO selfMessage(Long id) {
        Customers customers = new Customers();
        customers.setId(id);
        customers = customerMapper.GetByName(customers);
        CustomersVO customersVO = new CustomersVO();
        BeanUtils.copyProperties(customers,customersVO);

        Long sumRentals =rentalsMapper.selctCustomerIdSumById(id);
        customersVO.setSumRentals(sumRentals);
        return customersVO;
    }

    /**
     * 根据ID查询历史订单
     * @param id
     * @return
     */
    @Override
    public PageResult orderHistory(Long id) {
        //根据用户ID查询车辆租赁及归还时间车辆id
        List<Rentals> rentals = rentalsMapper.selectById(id);
        // 根据车辆id查询车辆基本信息
        List<OrderVO> voList = new ArrayList<>();//历史订单


        for(Rentals rental:rentals) {
            OrderVO orderVO = new OrderVO();
            Cars cars = carMapper.selectById(rental.getCustomerId());
            BeanUtils.copyProperties(cars,orderVO);
            orderVO.setRentalStartData(rental.getRentalStartDate());
            orderVO.setRentalEndData(rental.getRentalEndDate());
            voList.add(orderVO);
        }


        Long sumRentals = rentalsMapper.selctCustomerIdSumById(id);
        return new PageResult(sumRentals,voList);
    }
}
